package com.newland.mesdk.demo.fragment;

import java.util.concurrent.TimeUnit;

import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

import com.newland.mesdk.demo.main.R;
import com.newland.mtype.ModuleType;
import com.newland.mtype.event.DeviceEventListener;
import com.newland.mtype.module.common.cardreader.OpenCardReaderEvent;
import com.newland.mesdk.demo.interfaceimpl.CardReaderInterfaceImpl;
import com.newland.mesdk.demo.main.MainActivity;
import com.newland.mesdk.demo.util.Const.MessageTag;

/**
 * Created by YJF 读卡器模块
 */
public class CardReaderFragment extends Fragment implements OnClickListener {
	private Button btnOpenNotBlock, btnCancle, btnClose;
	private CardReaderInterfaceImpl cardReaderInterfaceImpl;
	private MainActivity mainActivity;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.fragment_cardreader, null);
		mainActivity = (MainActivity) getActivity();
		btnOpenNotBlock = (Button) view.findViewById(R.id.btn_cardreader_open_notboclk);
		btnOpenNotBlock.setOnClickListener(this);
		btnCancle = (Button) view.findViewById(R.id.btn_cardreader_cancle);
		btnCancle.setOnClickListener(this);
		btnClose = (Button) view.findViewById(R.id.btn_cardreader_close);
		btnClose.setOnClickListener(this);

		return view;
	}

	@Override
	public void onClick(View v) {
		if (mainActivity.isDeviceInit()) {
			mainActivity.connectDevice();
			cardReaderInterfaceImpl = new CardReaderInterfaceImpl();
			switch (v.getId()) {
			case R.id.btn_cardreader_open_notboclk:
				if (mainActivity.processingisLocked()) {
					mainActivity.showMessage("请先完成或撤销当前操作...", MessageTag.ERROR);
				} else {
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
								mainActivity.processingLock();
								mainActivity.showMessage("请刷卡或者插入IC卡..." + "\r\n", MessageTag.TIP);
								cardReaderInterfaceImpl.openCardReader("请刷卡或者插入IC卡", new ModuleType[] { ModuleType.COMMON_SWIPER,
										ModuleType.COMMON_ICCARDREADER }, null, true, true, 60, TimeUnit.SECONDS,
										new DeviceEventListener<OpenCardReaderEvent>() {
											@Override
											public void onEvent(OpenCardReaderEvent openCardReaderEvent, Handler handler) {
												if (openCardReaderEvent.isSuccess()) {
													mainActivity.showMessage("读卡器开启成功" + "\r\n", MessageTag.NORMAL);
													mainActivity.processingUnLock();
												} else if (openCardReaderEvent.isUserCanceled()) {
													mainActivity.showMessage("取消开启读卡器" + "\r\n", MessageTag.NORMAL);
													mainActivity.processingUnLock();
												} else if (openCardReaderEvent.isFailed()) {
													mainActivity.showMessage("读卡器开启失败" + "\r\n", MessageTag.NORMAL);
													mainActivity.processingUnLock();
												}
											}

											@Override
											public Handler getUIHandler() {
												return null;
											}
										});

							} catch (Exception e) {
								e.printStackTrace();
								mainActivity.showMessage("读卡器开启异常：" + "\r\n", MessageTag.ERROR);
								mainActivity.showMessage(e.getMessage() + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}
						}
					}).start();
				}

				break;
			case R.id.btn_cardreader_cancle:
				new Thread(new Runnable() {

					@Override
					public void run() {
						try {
							cardReaderInterfaceImpl.cancelCardRead();
							mainActivity.showMessage("撤销读卡操作：" + "成功" + "\r\n", MessageTag.NORMAL);
							mainActivity.processingUnLock();
						} catch (Exception e) {
							mainActivity.showMessage("撤销读卡操作 异常：" + e + "\r\n", MessageTag.ERROR);
						}

					}
				}).start();

				break;
			case R.id.btn_cardreader_close:
				new Thread(new Runnable() {

					@Override
					public void run() {
						try {
							mainActivity.processingUnLock();
							cardReaderInterfaceImpl.closeCardReader();
							mainActivity.showMessage("关闭读卡器：" + "成功" + "\r\n", MessageTag.NORMAL);
							mainActivity.processingUnLock();
						} catch (Exception e) {
							mainActivity.showMessage("关闭读卡器 异常：" + e + "\r\n", MessageTag.ERROR);
							mainActivity.processingUnLock();
						}
					}
				}).start();
                                                                                                                                                                                                                                                                                                                                                                          
				break;

			default:
				break;
			}
		} else {
			mainActivity.showMessage("设备未初始化，请先初始化！" + "\r\n", MessageTag.TIP);
		}

	}

}
